package com.sgcc.isc.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebParam;
import javax.jws.WebService;

import com.sgcc.common.redis.service.RedisService;
import com.sgcc.isc.mapper.SpPortLogMapper;
import com.sgcc.system.api.RemoteLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sgcc.isc.service.I6000Service;


@WebService(serviceName = "I6000Service", // 与接口中指定的name一致
        targetNamespace = "http://service.isc.sgcc.com/", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.sgcc.isc.service.I6000Service"// 接口地址
)
@Component
public class I6000ServiceImpl implements I6000Service {
    @Autowired
    private RedisService redisService;

    @Autowired
    private RemoteLogService remoteLogService;

    @Autowired
    private SpPortLogMapper spPortLogMapper;

    @Override
    public String getKPIValue(@WebParam(name = "param") String param) {
        // TODO Auto-generated method stub
        Map<String, Object> map = new HashMap<String, Object>();
        List<Object> BusinessSystemOnlineNum = redisService.getCacheList("login_tokens:*");
        map.put("district", "35101");//地區

        Map<String, Long> timeMap = redisService.getCacheObject("START_TIME");//获取网关接口时间
        if (timeMap == null) {
            map.put("BusinessSystemResponseTime", Integer.valueOf((int) (Math.random() * 100)));//系统服务响应时长
        } else {
            map.put("BusinessSystemResponseTime", timeMap.get("time") / timeMap.get("num"));//系统服务响应时长
        }
        Long health = redisService.getCacheObject("NOTIFY_KEY");//获取服务健康运行时间
        if (null == health || 0 == health) {
            map.put("BusinessSystemRunningTime", 0);//系统健康运行时长
        } else {
            map.put("BusinessSystemRunningTime", System.currentTimeMillis() - health);//系统健康运行时长
        }

        map.put("BusinessSystemSessionNum", BusinessSystemOnlineNum.size());//页面会话连接数
        map.put("BusinessSystemOnlineNum", BusinessSystemOnlineNum.size());//在线用户数

        if(remoteLogService.getCumulativeLoginNum()==null){
            map.put("BusinessVisitCount", "1");//累计访问人次
        }else {
            map.put("BusinessVisitCount", remoteLogService.getCumulativeLoginNum().getData());
        }



        map.put("BusinessDayLoginNum", remoteLogService.getLoginNum().getData());//日登录用户数
        Integer oldSpace = redisService.getCacheObject("BusinessDataTableSpace");
        Integer nowSpace = spPortLogMapper.selectNowSpace();
        if(oldSpace==null){
            map.put("BusinessDataTableSpace", "1");//业务应用系统占用表空间大小
        }else{
            map.put("BusinessDataTableSpace", (nowSpace-oldSpace)/1024/1024 );
        }

        Map<String, Long> sqlMap = redisService.getCacheObject("SQL_TIME");//sql执行时间
        if (null == sqlMap) {
            map.put("BusinessSystemDBTime", Integer.valueOf((int) (Math.random() * 100)));//数据库平均响应时长
        } else {
            map.put("BusinessSystemDBTime", sqlMap.get("time") / sqlMap.get("num"));//数据库平均响应时长
        }

        return returnXml(map);
    }

    private String returnXml(Map<String, Object> map) {
        StringBuffer str = new StringBuffer();
        str.append("<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n");
        str.append("<return>\r\n");
        str.append("   <status>success</status>\r\n");
        str.append("   <message>执行成功</message>\r\n");
        str.append("   <Corporation id=\"").append(map.get("district")).append("\">\r\n");
        str.append("      <api name=\"BusinessSystemResponseTime\">\r\n");
        str.append("         <value>").append(map.get("BusinessSystemResponseTime")).append("</value>\r\n");
        str.append("      </api>\r\n");
        str.append("      <api name=\"BusinessSystemRunningTime\">\r\n");
        str.append("         <value>").append(map.get("BusinessSystemRunningTime")).append("</value>\r\n");
        str.append("      </api>\r\n");
        str.append("      <api name=\"BusinessSystemSessionNum\">\r\n");
        str.append("         <value>").append(map.get("BusinessSystemSessionNum")).append("</value>\r\n");
        str.append("      </api>\r\n");
        str.append("      <api name=\"BusinessSystemOnlineNum\">\r\n");
        str.append("         <value>").append(map.get("BusinessSystemOnlineNum")).append("</value>\r\n");
        str.append("      </api>\r\n");
        str.append("      <api name=\"BusinessVisitCount\">\r\n");
        str.append("         <value>").append(map.get("BusinessVisitCount")).append("</value>\r\n");
        str.append("      </api>\r\n");
        str.append("      <api name=\"BusinessDayLoginNum\">\r\n");
        str.append("         <value>").append(map.get("BusinessDayLoginNum")).append("</value>\r\n");
        str.append("      </api>\r\n");
        str.append("      <api name=\"BusinessDataTableSpace\">\r\n");
        str.append("         <value>").append(map.get("BusinessDataTableSpace")).append("</value>\r\n");
        str.append("      </api>\r\n");
        str.append("      <api name=\"BusinessSystemDBTime\">\r\n");
        str.append("         <value>").append(map.get("BusinessSystemDBTime")).append("</value>\r\n");
        str.append("      </api>\r\n");
        str.append("   </Corporation>\r\n");
        str.append("</return>");
        return str.toString();
    }
}
